package com.example.springboot.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.stereotype.Component;

/**
 * @description: redis消息发布
 * @author: Yuwl
 * @create: 2020-04-07 11:37
 **/
@Component
public class PublishMessage {

	private static final Logger LOGGER = LoggerFactory.getLogger(PublishMessage.class);

	@Autowired
	private RedisTemplate redisTemplate;
	@Autowired
	private ChannelTopic channelTopic;

	public void publishTopicMessage(JSONObject json) {
		redisTemplate.convertAndSend(channelTopic.getTopic(), JSON.toJSONString(json));
		LOGGER.info("======================>>发布消息成功, json: {}", json);
	}
}
